﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using MYNAMESPACE;

namespace final_project
{
    public partial class BAYMACHIN : Form
    {
        private connect_db con;
        public string nm;
        public string passvalue
        {

            get
            {
                return nm;
            }
            set
            {
                nm = value;
            }
        }

        public string nm1;
        public string passvalue1
        {

            get
            {
                return nm1;
            }
            set
            {
                nm1 = value;
            }
        }
        public BAYMACHIN()
        {
            InitializeComponent();
        }

        private void BAYMACHIN_Load(object sender, EventArgs e)
        {
            ID.Text = nm;
            CO_ID.Text = nm1;
            con = new connect_db();
            user us = new user();
            us.setnational_id(Int32.Parse(ID.Text));
            con.sqlquery("select balance from userdata where id=(@namep)");
            con.cmd.Parameters.Add("@namep", us.getnational_id());
            foreach (DataRow dr in con.QueryEx().Rows)
            {

                User_balance.Text = dr[0].ToString();


            }

            /////////////////////////////////
            con = new connect_db();
            user US = new user();
            US.setnational_id(Int32.Parse(CO_ID.Text));
            con.sqlquery("select balance from userdata where id=(@namep)");
            con.cmd.Parameters.Add("@namep", US.getnational_id());
            foreach (DataRow dr in con.QueryEx().Rows)
            {

                Company_balance.Text = dr[0].ToString();


            }
             
        }

        private void button1_Click(object sender, EventArgs e)
        {

            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 
            
            
            user us = new user();
            user US = new user();
           
            if (Value.Text.Length == 0)
            {

                MessageBox.Show("must have value");

            }
            else {
                us.setnational_id(Int32.Parse(ID.Text));
                US.setnational_id(Int32.Parse(CO_ID.Text));
                us.setvalue(double.Parse(Value.Text));
            
                us.setbalance(double.Parse(User_balance.Text));
                us.withdrwal(double.Parse(Value.Text));
                con = new connect_db();
               
                con.sqlquery("update userdata set balance =(@namep1) where id= (@namep) ");
                con.cmd.Parameters.Add("@namep", us.getnational_id());
                con.cmd.Parameters.Add("@namep1", us.getbalance());
                con.sqlnonex();

                US.setbalance(double.Parse(Company_balance.Text));
                US.BAY_deposite(double.Parse(Value.Text));
                con.sqlquery("update userdata set balance = (@namep1) where id= (@namep) ");
                con.cmd.Parameters.Add("@namep", US.getnational_id());
                con.cmd.Parameters.Add("@namep1", US.getbalance());
                con.sqlnonex();
                MessageBox.Show("Done");
                this.Hide();
                BayAuto BB = new BayAuto();
                BB.ShowDialog();
              
            }
            }

        private void Value_KeyPress(object sender, KeyPressEventArgs e)
        {
            char cb = e.KeyChar;
            if (!char.IsDigit(cb) && cb != 8)
            {

                e.Handled = true;

            }
        }

        private void Value_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(Value.Text))
            {

                e.Cancel = true;
                Value.Focus();
                errorProvider1.SetError(Value, "Value Must Be Entered !");

            }
            else if (!Regex.IsMatch(Value.Text, @"^\d+$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Value, "No Negative Numbers");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Value, "");
            }
        }
        }
    }

